package cn.com.yx.ms.controller;

import cn.com.yx.ms.domain.LoginModel;
import cn.com.yx.ms.service.ISysService;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.view.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.security.MessageDigest;

/**
 * 应用中心
 */
@RestController
@RequestMapping(value = "login")
@Api(value = "登录接口", description = "用户登录使用的接口")
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Resource
    private MessageSource messageSource;

    @Resource
    private ISysService iSysService;


    @PostMapping()
    @ApiOperation(value = "用户登录", notes = "用户登录")
    @ApiImplicitParam(paramType = "body", name = "userLoginModel", dataType = "Map", required = true, value = "用户登录实体")
    public ApiResult login(@RequestBody @Validated LoginModel userLoginModel, BindingResult bindingResult, HttpServletRequest request) throws Exception {
        if (null != userLoginModel.getValidCode()) {
            //进入验证码判断
            if (!userLoginModel.getValidCode().equals(iSysService.getValidCode(userLoginModel.getUuid()))) {
                return new ApiResult<>(AppConstant.MSG_FAILURE, false, "", new ApiResult(AppConstant.MSG_FAILURE, false, "验证码错误!"));
            }
        }
//        if (userLoginModel != null && userLoginModel.getIsCookie() != null && userLoginModel.getIsCookie().equals("true")) {
//            //使用cookie中的密码登录页面
//            return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", iSysService.cookieLogin(userLoginModel.getUsername(), userLoginModel.getPassword(), userLoginModel.getIsLogin(), request));
//        }
        //登录验证
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", iSysService.login(userLoginModel.getUsername(), userLoginModel.getPassword(), userLoginModel.getIsLogin(), request,userLoginModel.getIsCookie(),userLoginModel.getPassword()));
    }

}